﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace QuanLyKhachSan
{
    public partial class frmQuanLyThuePhong : DevExpress.XtraEditors.XtraForm
    {
        public frmQuanLyThuePhong()
        {
            InitializeComponent();
        }

        DataClassesKhachSanDataContext db = new DataClassesKhachSanDataContext(SystemInfo.pub_ConnString);

        private void btThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmDangKyThuePhong frmthuephong = new frmDangKyThuePhong();
            frmthuephong.testThuePhongTuQuanLyThuePhong = true;
            frmthuephong.frmquanlythuephong = this;
            frmthuephong.ShowDialog();
        }

        private void gvThuePhong_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "")
            {
                if (e.RowHandle >= 0)
                {
                    e.DisplayText = (e.RowHandle + 1).ToString();
                }
            } 
        }

        public   void fillData()
        {
            //DataClassesKhachSanDataContext db1 = new DataClassesKhachSanDataContext(SystemInfo.pub_ConnString);
            var thuephong = db.DanhSachThuePhong();
            gcThuePhong.DataSource = thuephong;
        }

        private void frmQuanLyThuePhong_Load(object sender, EventArgs e)
        {
            this.fillData();
        }

        private void btXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            DialogResult result = XtraMessageBox.Show("Bạn có chắc muốn xóa?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
            if (result == DialogResult.Yes)
            {

                if (gvThuePhong.FocusedRowHandle < 0)
                {
                    if (gvThuePhong.GetGroupRowDisplayText(gvThuePhong.FocusedRowHandle).ToString().Contains("Khách hàng"))
                    {
                        //MessageBox.Show(gvThuePhong.GetGroupRowValue(gvThuePhong.FocusedRowHandle, gridColumn5).ToString());
                        try
                        {
                            string khachhang = gvThuePhong.GetGroupRowValue(gvThuePhong.FocusedRowHandle, gridColumn5).ToString();
                            int ngancach = khachhang.IndexOf('(');


                            string hoten = khachhang.Substring(0, ngancach - 1).Trim();
                            string cmnd = khachhang.Substring(ngancach + 1, khachhang.Length - hoten.Length - 5).Trim();

                            var thuephong = from t in db.THUE_PHONGs
                                            where
                                                t.KHACH_HANG.KH_HOTEN == hoten
                                                && t.KHACH_HANG.KH_CMND == cmnd
                                            select t;
                            //kiem tra chi tiet va chuyen trang thai phong
                            var chitiet = thuephong.ToList ();
                            for (int i = 0; i < chitiet.Count; i++)
                            {
                                var ct = (from c in db.CHI_TIET_THUE_PHONGs
                                         where c.TP_ID == chitiet [i].TP_ID select c).ToList();
                                //chuyen trang thai phong o day
                                for (int j = 0; j < ct.Count; j++)
		                        {
                    			  var trangthai = db.PHONGs.Single(t => t.P_ID == ct [j].P_ID);
                                  trangthai.TT_ID = 1;
		                        }
                                                   
                            }


                            db.THUE_PHONGs.DeleteAllOnSubmit(thuephong);
                            db.SubmitChanges();
                            this.fillData();
                        }
                        catch (Exception)
                        {

                            throw;
                        }

                    }
                    else
                    {
                        //MessageBox.Show(gvThuePhong.GetGroupRowValue(gvThuePhong.FocusedRowHandle, gridColumn14).ToString());
                        try
                        {
                            var chitiet = from ct in db.CHI_TIET_THUE_PHONGs
                                          where ct.P_ID == int.Parse(gvThuePhong.GetGroupRowValue(gvThuePhong.FocusedRowHandle, gridColumn14).ToString())
                                          select ct;

                            db.CHI_TIET_THUE_PHONGs.DeleteAllOnSubmit(chitiet);

                            var phong = db.PHONGs.Single (p => p.P_ID == int.Parse(gvThuePhong.GetGroupRowValue(gvThuePhong.FocusedRowHandle, gridColumn14).ToString() ));
                            phong.TT_ID = 1;

                            db.SubmitChanges();
                            this.fillData();
                        }
                        catch (Exception)
                        {

                            throw;
                        }
                    }
                }
                else
                {
                    // MessageBox.Show(gvThuePhong.GetFocusedRowCellValue("CTTP_ID").ToString());

                    try
                    {
                        int khid;
                         
                        if (gvThuePhong.GetFocusedRowCellValue(gridColumn6).ToString () != "0" )
                        {
                            khid = int.Parse(gvThuePhong.GetFocusedRowCellValue(gridColumn6).ToString());
                            var trutai = from tt in db.TRU_TAIs
                                         where tt.KH_ID == khid
                                          && tt.CTTP_ID == int.Parse(gvThuePhong.GetFocusedRowCellValue("CTTP_ID").ToString())
                                         select tt;
                            db.TRU_TAIs.DeleteAllOnSubmit(trutai);
                            db.SubmitChanges();
                            this.fillData();
                        }
                        else
                        {
                            var trutai = from tt in db.TRU_TAIs
                                         where 
                                         tt.CTTP_ID == int.Parse(gvThuePhong.GetFocusedRowCellValue("CTTP_ID").ToString())
                                         select tt;
                            db.TRU_TAIs.DeleteAllOnSubmit(trutai);
                            db.SubmitChanges();
                            this.fillData();
                        }
                       
                     

                      
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }

            }
        }
        private void gvThuePhong_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            //MessageBox.Show(gvThuePhong.GetFocusedRowCellValue("CTTP_ID").ToString());
        }

        private void btTaiLai_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.fillData();
        }
    }
}